package com.lqk.shardingjdbcproj.strategy;

import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Collection;

/**
 * @author liqiankun
 * @date 2022/4/26 21:40
 * @description 分库策略
 **/
public class SubLibraryStrategy implements StandardShardingAlgorithm<Long> {

    public SubLibraryStrategy() {
    }

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> preciseShardingValue) {
        //availableTargetNames: m0,db3
        //shardingValue: {"logicTableName":"t_order","columnName":"order_id","value":396416249350848512}
        //collection:["t_order_1","t_order_2"],preciseShardingValue:{"logicTableName":"t_order","columnName":"order_id","value":396416249350848512}
        //name为两张订单表 t_order_1 和 t_order_2
//        for (String name : availableTargetNames) {
//            //订单号取模加1 与 订单表t_order_1 和 t_order_2的尾号做比对，如相等，就直接返回t_order_1 或 t_order_2
//            if (name.endsWith(String.valueOf(preciseShardingValue.getValue() % 2 + 1))) {
//                return name;
//            }
//        }
        if (preciseShardingValue.getValue()>10){
            return "m0";
        }else {
            return "db3";
        }
    }

    @Override
    public Collection<String> doSharding(Collection collection, RangeShardingValue rangeShardingValue) {
        return null;
    }

    @Override
    public void init() {

    }

    @Override
    public String getType() {
        return null;
    }
}
